﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNet.Model;
using DotNet.Common;
using DotNet.Data.Factory;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace DotNet.Data
{

    public class productClassDal
    {
        IDBHelp db = DBHelp.Create();
        Hashtable htSyn = Hashtable.Synchronized(new Hashtable());
        StringBuilder strSql = new StringBuilder();

        #region 是否存在该记录
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int pClassId)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select count(1) from productClass where pClassId=@pClassId ");
            htSyn.Add("pClassId", pClassId);
            return db.IsExists(strSql.ToString(), htSyn);
        }
        #endregion

        #region 是否存在子类
        /// <summary>
        /// 是否存在子类
        /// </summary>
        public bool ExistsChildrens(string ClassId)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select count(1) from productClass where pfid=@ClassId ");
            htSyn.Add("ClassId", ClassId);
            return db.IsExists(strSql.ToString(), htSyn);
        }
        #endregion

        #region 获得产品类别列表
        /// <summary>
        /// 获得产品类别列表
        /// </summary>
        /// <returns></returns>
        public IList<productClass> GetModel()
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select pClassId,pClassName,pTag,pCImages,pCInfo,pFid,cSort from productClass order by csort asc");
            return getProductsBySql(strSql.ToString());
        }
        #endregion

        #region 获得产品类别信息
        /// <summary>
        /// 获得产品类别数据列表
        /// </summary>
        /// <returns></returns>
        public DataTable GetDataTable()
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select pClassId,pClassName,pTag,pCImages,pCInfo,pFid,cSort from productClass order by csort asc  ");
            return db.GetDataTable(strSql.ToString());
        }
        #endregion

        #region 根据父ID获得产品类别信息
        /// <summary>
        /// 根据父ID获得产品类别信息
        /// </summary>
        /// <returns></returns>
        public DataTable GetDataTable(int top,int fid)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select top "+top+" pClassId,pClassName,pTag,pCImages,pCInfo,pFid,cSort from productClass where pFid=@fid order by csort asc  ");
            htSyn.Add("@fid",fid);
            return db.GetDataTable(strSql.ToString(),htSyn);
        }
        #endregion

        #region 获得父类信息
        /// <summary>
        /// 获得父类信息
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="ParentID"></param>
        /// <returns></returns>
        public DataView GetDataView(DataTable dt,int ParentID)
        {
            DataView dv = new DataView(dt);
            dv.RowFilter = "[pFid]=" + ParentID;
            return dv;
        }
        #endregion

        #region 根据类别ID获得类别名称
        /// <summary>
        /// 获得父类信息
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="ParentID"></param>
        /// <returns></returns>
        public DataView GetModel(DataTable dt, int classid)
        {
            DataView dv = new DataView(dt);
            dv.RowFilter = "[pClassId]=" + classid;
            return dv;
        }
        #endregion

        #region 根据产品类别号查询实体
        /// <summary>
        /// 根据产品类别号查询实体
        /// </summary>
        /// <param name="pClassId"></param>
        /// <returns></returns>
        public productClass GetModel(int pClassId)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select pClassId,pClassName,pTag,pCImages,pCInfo,pFid,cSort from productClass ");
            strSql.Append(" where pClassId=@pClassId ");
            htSyn.Add("@pClassId", pClassId);
            DataTable dt = db.GetDataTable(strSql.ToString(), htSyn);
            productClass productclass = new productClass();
            if (dt.Rows.Count>0)
            {
                
                productclass.pClassId = Convert.ToInt32(dt.Rows[0]["pClassId"]);
                productclass.pClassName = dt.Rows[0]["pClassName"].ToString();
                productclass.pTag = dt.Rows[0]["pTag"].ToString();
                productclass.pCImages = dt.Rows[0]["pCImages"].ToString();
                productclass.pCInfo = dt.Rows[0]["pCInfo"].ToString();
                productclass.pFid = Convert.ToInt32(dt.Rows[0]["pFid"]);
                productclass.cSort = Convert.ToInt32(dt.Rows[0]["cSort"]);
            }           
            return productclass;
        }
        #endregion

        #region 修改产品类别信息
        /// <summary>
        /// 修改产品类别信息
        /// </summary>
        /// <param name="productclass"></param>
        /// <returns></returns>
        public bool Update(productClass productclass)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("update productClass set ");
            strSql.Append("pClassName=@pClassName,");
            strSql.Append("pTag=@pTag,");
            strSql.Append("pCImages=@pCImages,");
            strSql.Append("pCInfo=@pCInfo,");
            strSql.Append("pFid=@pFid,");
            strSql.Append("cSort=@cSort");
            strSql.Append(" where pClassId=@pClassId ");
            htSyn.Add("@pClassId",productclass.pClassId);
            htSyn.Add("@pClassName",productclass.pClassName);
            htSyn.Add("@pTag",productclass.pTag);
            htSyn.Add("@pCImages",productclass.pCImages);
            htSyn.Add("@pCInfo",productclass.pCInfo);
            htSyn.Add("@pFid",productclass.pFid);
            htSyn.Add("@cSort", productclass.cSort);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 删除产品类别根据产品类别号
        /// <summary>
        /// 删除产品类别根据产品类别号
        /// </summary>
        /// <param name="pClassId"></param>
        /// <returns></returns>
        public bool Delete(int pClassId)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("delete from productClass where pClassId=@pClassId");
            htSyn.Add("pClassId", pClassId);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 批量删除产品类别根据产品类别号
        /// <summary>
        /// 批量删除产品类别根据产品类别号
        /// </summary>
        /// <param name="pClassId"></param>
        /// <returns></returns>
        public bool Delete(string[]pClassid)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("delete from productClass where");
            foreach (string pcid in pClassid)
            {
                strSql.AppendFormat(" pClassId={0} or",pcid);
            }
            strSql = strSql.Remove(strSql.Length-1,1);
            strSql = strSql.Remove(strSql.Length-1,1);
            if (db.ExecuteNonQuery(strSql.ToString()) > 0)
            {
                return true;
            }
            return false;
        }
               #endregion

        #region 批量移动产品类别根据产品父类别号
        /// <summary>
        /// 批量移动产品类别根据产品父类别号
        /// </summary>
        /// <param name="id"></param>
        ///  <param name="classid"></param>
        /// <returns></returns>
        public bool Update(string[]id,string classid)
        {
            strSql.Remove(0, strSql.Length);
            strSql.Append("update productClass set");
            strSql.AppendFormat(" pFid={0}",classid);
            strSql = strSql.Append(" where ");
            foreach (string cid in id)
            {
                strSql.AppendFormat(" pclassid={0} or",cid);
            }
            strSql=strSql.Remove(strSql.Length - 1, 1);
            strSql = strSql.Remove(strSql.Length - 1, 1);
            if (db.ExecuteNonQuery(strSql.ToString()) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 添加产品类别
        /// <summary>
        /// 添加产品类别
        /// </summary>
        /// <param name="productclass"></param>
        /// <returns></returns>
        public bool Insert(productClass productclass)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("insert into productClass([pClassName],[pTag],[pCImages],[pCInfo],[pFid],[cSort]) values (@pClassName,@pTag,@pCImages,@pCInfo,@pFid,@cSort)");
            htSyn.Add("@pClassName", productclass.pClassName);
            htSyn.Add("@pTag", productclass.pTag);
            htSyn.Add("@pCImages", productclass.pCImages);
            htSyn.Add("@pCInfo", productclass.pCInfo);
            htSyn.Add("@pFid", productclass.pFid);
            htSyn.Add("@cSort", productclass.cSort);
            if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region 根据SQL语句获取集合
        /// <summary>
        ///根据SQL语句获取集合
        /// </summary>
        public IList<productClass> getProductsBySql(string strSql)
        {
            IList<productClass> list = new List<productClass>();
            DataTable dt = db.GetDataTable(strSql);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    productClass productclass = new productClass();
                    productclass.pClassId = Convert.ToInt32(dr["pClassId"]);
                    productclass.pClassName = dr["pClassName"].ToString();
                    productclass.pTag = dr["pTag"].ToString();
                    productclass.pCImages = dr["pCImages"].ToString();
                    productclass.pCInfo = dr["pCInfo"].ToString();
                    productclass.pFid = Convert.ToInt32(dr["pFid"]);
                    productclass.cSort = Convert.ToInt32(dr["cSort"]);
                    list.Add(productclass);
                }
            }
            return list;
        }
        #endregion
    }
}
